<?php


namespace app\admin\model;


use think\Db;

class ServiceBuriedModel extends BaseModel
{
    protected $name='service_buried';

    protected $pk='id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public  function doAddData($data=[],$mw=[]){
        $deceased = $data['deceased'];
        $handle = $data['handle'];
        $this->startTrans();
        try {
            if (empty($handle['dealer_user'])){
                $this->error='请输入人办理姓名';
                $this->rollback();
                return false;
            }
            if (empty($handle['dealer_phone'])){
                $this->error='请输入办人理手机号';
                $this->rollback();
                return false;
            }
            if (!cmf_check_mobile($handle['dealer_phone'])) {
                $this->error= "请输入正确的手机格式!";
                $this->rollback();
                return false;
            }
            if (empty($handle['dealer_cart_no'])){
                $this->error='请输入办理人证件号码';
                $this->rollback();
                return false;
            }
            $dead_name='';
            $dead_id='';
            $is_hhd=[];
            $funeral_time=[];
            $buried_inst = [
                'order_id' => $mw['order_id'],
                'tomb_id' => $mw['tomb_id'],
                'tomb_num' => $mw['tomb_num'],
                'tomb_address' => $mw['tomb_region'],
                'dealer_user' => $handle['dealer_user'],
                'cart_type' => $handle['cart_type'],
                'dealer_cart_no' => $handle['dealer_cart_no'],
                'dealer_phone' => $handle['dealer_phone'],
                'dealer_address' => $handle['dealer_address'],
                'buyer_name'=>$mw['buyer_name'],
                'funeral_type' => $mw['funeral'],
                'customer_id' => $mw['customer_id'],
            ];

            if ($handle['service_status'] != 3) {
                $buried_id = $handle['buried_id'];
                if (!empty($buried_id)) {
                    $this->update($buried_inst, ['id' => $buried_id]);
                } else {
                    $buried_inst['handle_id'] = cmf_get_current_admin_id();
                    $buried_inst['handle_name'] = cmf_get_admin_name();
                    $buried_inst['service_status'] = 1;
                    $buried_inst['buried_time'] = time();
                    $buried_inst['customer_id'] = $mw['customer_id'];
                    $buried_inst['add_user'] = cmf_get_current_admin_id();
                    $buried_info = $this->create($buried_inst);
                    $buried_id = $buried_info['id'];
                }
                $flag = false;
                foreach ($deceased as $key => &$value) {
                    if (!empty($value['deceased_name'])) {
                        $flag = true;
                    }
                }
                if(!$flag){
                    $this->error= "至少填写一位逝者信息!";
                    $this->rollback();
                    return false;
                }
                foreach ($deceased as $key => &$value) {
                    if(!empty($value['deceased_name'])){
                        if(empty($value['funeral_time'])){
                            $this->error= "落葬时间不能为空!";
                            $this->rollback();
                            return false;
                        }
                    }
                    if ($value['buied_status'] != 3) {
                        if ($dead_name){
                            $dead_name=$dead_name.','.$value['deceased_name'];
                        }else{
                            $dead_name=$value['deceased_name'];
                        }
                        $deceased_id = $value['deceased_id'];
                        unset($value['deceased_id']);
                        if (!empty($deceased_id)) {
                            $funeral_time[] = $value['funeral_time'];
                            ArchivesDeceasedModel::update($value, ['deceased_id' => $deceased_id]);
                            if ($value['cremation_certificate']){
                                $is_hhd[] = 1;
                            }else{
                                $is_hhd[] = 0;
                            }
                            if ($dead_id){
                                $dead_id = $dead_id.','.$deceased_id;
                            }else{
                                $dead_id = $deceased_id;
                            }
                        } else {
                            $value['buried_id'] = $buried_id;
                            if ($value['deceased_name']) {
                                if ($value['cremation_certificate']){
                                    $is_hhd[] = 1;
                                }else{
                                    $is_hhd[] = 0;
                                }
                                $funeral_time[] = $value['funeral_time'];
                                $buried_time = $value['funeral_time'];
                                $deceased_list = ArchivesDeceasedModel::create($value);
                                $deceased_id = $deceased_list['deceased_id'];
                                if ($dead_id){
                                    $dead_id = $dead_id.','.$deceased_id;
                                }else{
                                    $dead_id = $deceased_id;
                                }
                            }
                        }
                    }
                }
            }else{
                $buried_inst['handle_id'] = cmf_get_current_admin_id();
                $buried_inst['handle_name'] = cmf_get_admin_name();
                $buried_inst['service_status'] = 1;
                $buried_inst['customer_id'] = $mw['customer_id'];
                $buried_inst['buried_time'] = time();
                $buried_inst['add_user'] = cmf_get_current_admin_id();
                $buried_info = $this->create($buried_inst);
                $buried_id = $buried_info['id'];
                foreach ($deceased as $key => &$value) {
                    if ($value['buied_status'] != 3) {
                        if ($dead_name){
                            $dead_name=$dead_name.','.$value['deceased_name'];
                        }else{
                            $dead_name=$value['deceased_name'];
                        }
                        $deceased_id = $value['deceased_id'];
                        unset($value['deceased_id']);
                        if (!empty($deceased_id)) {
                            $funeral_time[] = $value['funeral_time'];
                            ArchivesDeceasedModel::update($value, ['deceased_id' => $deceased_id]);
                            if ($value['cremation_certificate']){
                                $is_hhd[] = 1;
                            }else{
                                $is_hhd[] = 0;
                            }
                            if ($dead_id){
                                $dead_id = $dead_id.','.$deceased_id;
                            }else{
                                $dead_id = $deceased_id;
                            }
                        } else {
                            $value['buried_id'] = $buried_id;
                            if ($value['deceased_name']) {
                                if ($value['cremation_certificate']){
                                    $is_hhd[] = 1;
                                }else{
                                    $is_hhd[] = 0;
                                }
                                $funeral_time[] = $value['funeral_time'];
                                $deceased_list = ArchivesDeceasedModel::create($value);
                                $deceased_id = $deceased_list['deceased_id'];
                                if ($dead_id){
                                    $dead_id = $dead_id.','.$deceased_id;
                                }else{
                                    $dead_id = $deceased_id;
                                }
                            }
                        }
                    }
                }
            }
            $this->update(['dead_name'=>$dead_name,'dead_id'=>$dead_id,'is_hhd'=>max($is_hhd),'buried_time'=>date_to_unixtime(min($funeral_time)),'funeral_time'=>date_to_unixtime(min($funeral_time))],['id'=>$buried_id]);
            if (isset($handle['is_sms'])){
                //发送短信
                $iSenf = sendNoticeSms(1,$handle['dealer_phone'],['burial_time'=>min($funeral_time)],$errorMsg);
                if ($iSenf==false){
                    return $this->error($errorMsg);
                }
            }
            $this->commit();
            return $buried_id;
        }catch(\Exception $e){
            $this->error='操作失败';
            $this->rollback();
            return false;
        }
    }

    public function doAddYsData($data=[],$mw=[]){
        $deceased = $data['deceased'];
        $handle = $data['handle'];
        $this->startTrans();
        try {
            if (empty($handle['dealer_user'])){
                $this->error='请输入人办理姓名';
                $this->rollback();
                return false;
            }
            if (empty($handle['dealer_phone'])){
                $this->error='请输入办人理手机号';
                $this->rollback();
                return false;
            }
            if (!cmf_check_mobile($handle['dealer_phone'])) {
                $this->error= "请输入正确的手机格式!";
                $this->rollback();
                return false;
            }
            if (empty($handle['dealer_cart_no'])){
                $this->error='请输入办理人证件号码';
                $this->rollback();
                return false;
            }
            $dead_name='';
            $dead_id='';
            $is_hhd=[]; 
            $funeral_time=[];
            $buried_inst = [
                'order_id' => $mw['order_id'],
                'tomb_id' => $mw['tomb_id'],
                'tomb_num' => $mw['tomb_num'],
                'tomb_address' => $mw['tomb_region'],
                'dealer_user' => $handle['dealer_user'],
                'cart_type' => $handle['cart_type'],
                'dealer_cart_no' => $handle['dealer_cart_no'],
                'dealer_phone' => $handle['dealer_phone'],
                'dealer_address' => $handle['dealer_address'],
                'funeral_type' => $mw['funeral'],
                'customer_id' => $mw['customer_id'],
                'mb_id' => $data['mb_id'],
            ];

            if ($handle['service_status'] != 3) {
                $buried_id = $handle['buried_id'];
                if (!empty($buried_id)) {
                    $this->update($buried_inst, ['id' => $buried_id]);
                } else {
                    $buried_inst['handle_id'] = cmf_get_current_admin_id();
                    $buried_inst['handle_name'] = cmf_get_admin_name();
                    $buried_inst['service_status'] = 1;
                    $buried_inst['buried_time'] = time();
                    $buried_inst['add_user'] = cmf_get_current_admin_id();
                    $buried_info = $this->create($buried_inst);
                    $buried_id = $buried_info['id'];
                }
                foreach ($deceased as $key => &$value) {
                    if ($value['buied_status'] != 3) {
                        if ($dead_name){
                            $dead_name=$dead_name.','.$value['deceased_name'];
                        }else{
                            $dead_name=$value['deceased_name'];
                        }
                        if ($value['cremation_certificate']){
                            $is_hhd[] = 1;
                        }else{
                            $is_hhd[] = 0;
                        }
                        $funeral_time[] = $value['funeral_time'];
                        $deceased_id = $value['deceased_id'];
                        unset($value['deceased_id']);
                        if (!empty($deceased_id)) {
                            if ($value['deceased_name']) {
                                $funeral_time[] = $value['funeral_time'];
                            }
                            $value['buried_id']=$buried_id;
                            ArchivesDeceasedModel::update($value, ['deceased_id' => $deceased_id]);
                            if ($dead_id){
                                $dead_id = $dead_id.','.$deceased_id;
                            }else{
                                $dead_id = $deceased_id;
                            }
                        } else {
                            $value['buried_id'] = $buried_id;
                            if ($value['deceased_name']) {
                                $funeral_time[] = $value['funeral_time'];
                                $deceased_list = ArchivesDeceasedModel::create($value);
                                $deceased_id = $deceased_list['deceased_id'];
                                if ($dead_id){
                                    $dead_id = $dead_id.','.$deceased_id;
                                }else{
                                    $dead_id = $deceased_id;
                                }
                            }
                        }
                    }
                }

            } else {
                $buried_inst['handle_id'] = cmf_get_current_admin_id();
                $buried_inst['handle_name'] = cmf_get_admin_name();
                $buried_inst['service_status'] = 1;
                $buried_inst['buried_time'] = time();
                $buried_inst['add_user'] = cmf_get_current_admin_id();
                $buried_info = $this->create($buried_inst);
                $buried_id = $buried_info['id'];
                foreach ($deceased as $key => &$value) {
                    if ($value['buied_status'] != 3) {
                        $deceased_id = $value['deceased_id'];
                        if ($dead_name){
                            $dead_name=$dead_name.','.$value['deceased_name'];
                        }else{
                            $dead_name=$value['deceased_name'];
                        }
                        if ($value['cremation_certificate']){
                            $is_hhd[] = 1;
                        }else{
                            $is_hhd[] = 0;
                        }

                        unset($value['deceased_id']);
                        if (!empty($deceased_id)) {
                            if ($value['deceased_name']) {
                                $funeral_time[] = $value['funeral_time'];
                            }
                            $value['buried_id'] = $buried_id;
                            ArchivesDeceasedModel::update($value, ['deceased_id' => $deceased_id]);
                            if ($dead_id){
                                $dead_id = $dead_id.','.$deceased_id;
                            }else{
                                $dead_id = $deceased_id;
                            }
                        } else {
                            $value['buried_id'] = $buried_id;
                            if ($value['deceased_name']) {
                                $funeral_time[] = $value['funeral_time'];
                                $deceased_list = ArchivesDeceasedModel::create($value);
                                $deceased_id = $deceased_list['deceased_id'];
                                if ($dead_id){
                                    $dead_id = $dead_id.','.$deceased_id;
                                }else{
                                    $dead_id = $deceased_id;
                                }
                            }
                        }
                    }
                }
            }
            $this->update(['dead_name'=>$dead_name,'dead_id'=>$dead_id,'is_hhd'=>max($is_hhd),'buried_time'=>date_to_unixtime(min($funeral_time)),'funeral_time'=>date_to_unixtime(min($funeral_time))],['id'=>$buried_id]);
            if (isset($handle['is_sms']) && !empty($funeral_time)){
                //发送短信
                $iSenf = sendNoticeSms(1,$handle['dealer_phone'],['burial_time'=>min($funeral_time)],$errorMsg);
                if ($iSenf==false){
                    return $this->error($errorMsg);
                }
            }
            $result = $buried_id;
            $this->commit();
        }catch(\Exception $e){
            $this->error='操作失败';
            $result = false;
            $this->rollback();
        }
        return $result;
    }
}